<template>
  <div class="order">
    <div class="order__price">
      实付金额 &yen;<b>{{ calculations.totalPrice }}</b>
    </div>
    <div class="order__btn" @click="handleSubmitClick(true)">提交订单</div>
  </div>
  <div class="mask" v-show="showConfirm" @click="handleSubmitClick(false)">
    <div class="mask__content" @click.stop>
      <div class="mask__content__title">确认离开收银台</div>
      <p>请尽快完成支付，否则将被取消</p>
      <div class="mask__content__btns">
        <div class="mask__content__btn mask__content__btn--first" @click="handleConfirmOrder(true)">
          取消订单
        </div>
        <div
          class="mask__content__btn mask__content__btn--second"
          @click="handleConfirmOrder(false)"
        >
          确认支付
        </div>
      </div>
    </div>
    <Toast v-if="show" :message="message" />
  </div>
</template>
<script>
  import { ref } from 'vue'
  import { useCommonCartEffect } from '@/effects/cartEffects'
  import { useRoute, useRouter } from 'vue-router' // 路由跳转方法
  import { useStore } from 'vuex'
  import { post } from '@/utils/request'
  import Toast, { useToastEffect } from '@/components/Toast/Toast'

  const useMakeOrderEffect = (shopName, productList, toastMsg) => {
    // 获取路由实例
    const router = useRouter()
    const store = useStore()

    /**
     * 下单交易逻辑：提交订单或订单取消
     */
    const handleConfirmOrder = async isCanceled => {
      try {
        debugger
        console.log('productList:')
        console.log(productList)
        const products = []
        for (const i in productList.value) {
          const product = productList.value[i]
          products.push({ id: product._id, num: product.count })
        }
        console.log('products:')
        console.log(products)
        const resultData = await post('/api/createOrder', {
          addressId: 'xxx', // 收货地址 id
          shopId: shopId, // 商店id
          shopName: shopName.value,
          isCanceled: isCanceled, // 订单是否被取消了
          products: products
        })
        console.log('post /api/createOrder：')
        console.log(resultData)
        if (resultData?.code === 200) {
          // 如果订单提交成功，需要清空购物车中的商品数据
          store.commit('clearCartData', { shopId })
          router.push({ name: 'OrderList' })
        } else {
          toastMsg('订单提交失败！')
        }
      } catch (e) {
        toastMsg('请求失败！')
      }
    }
    return { handleConfirmOrder }
  }
  /**
   * 确认订单弹窗控制
   */
  const useShowMakeEffect = () => {
    const showConfirm = ref(false)

    const handleSubmitClick = status => {
      showConfirm.value = status
    }

    return {
      showConfirm,
      handleSubmitClick
    }
  }
  export default {
    name: 'Order',
    components: {
      Toast
    },
    setup() {
      const route = useRoute()
      const shopId = route.params.shopId // 店铺id

      const { shopName, calculations, productList } = useCommonCartEffect(shopId)
      const { show, message, toastMsg } = useToastEffect()
      const { handleConfirmOrder } = useMakeOrderEffect(shopName, productList, toastMsg)
      const { handleSubmitClick, showConfirm } = useShowMakeEffect()

      return {
        show,
        message,
        showConfirm,
        handleSubmitClick,
        calculations,
        handleConfirmOrder
      }
    }
  }
</script>
<style lang="scss" scoped>
  @import '@/style/viriables.scss';
  .order {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    box-sizing: border-box; //往内塞入border
    line-height: 0.49rem;
    height: 0.49rem;
    border-top: 0.01rem solid $content-bg-color;
    background: $bg-color;
    &__price {
      flex: 1;
      text-indent: 0.24rem;
      font-size: 0.14rem;
      color: $content-font-color;
    }
    &__btn {
      width: 0.98rem;
      background-color: #4fb0f9;
      text-align: center;
      color: $bg-color;
      font-size: 0.14rem;
      // 去掉a标签的下划线
      a {
        color: $bg-color;
        text-decoration: none; //去掉文本修饰
      }
    }
  }
  .mask {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    top: 0;
    z-index: 1;
    background: rgba(0, 0, 0, 0.5);
    &__content {
      position: absolute;
      top: 50%;
      left: 50%;
      width: 3rem;
      height: 1.56rem;
      transform: translate(-50%, -50%);
      background: #fff;
      border-radius: 0.04rem;
      text-align: center;
      &__title {
        margin: 0.24rem 0 0 0;
        font-size: 0.18rem;
        color: #333;
      }
      &__desc {
        margin: 0.08rem 0 0 0;
        font-size: 0.14rem;
        color: #666;
      }
      &__btns {
        display: flex;
        margin: 0.24rem 0.58rem;
      }
      &__btn {
        flex: 1;
        width: 0.8rem;
        line-height: 0.32rem;
        border: 0.01rem solid #4fb0f9;
        border-radius: 0.16rem;
        font-size: 0.14rem;
        &--first {
          margin-right: 0.12rem;
          color: #4fb0f9;
        }
        &--second {
          margin-left: 0.12rem;
          color: #fff;
          background: #4fb0f9;
        }
      }
    }
  }
</style>
